package com.css.zfzx.data.primary.qhRiskLevelMapPolygonPG.repository.impl;


import com.css.zfzx.data.primary.qhProbabilityEarthquakeDangerPG.repository.entity.QhProbabilityEarthquakeDangerPGEntity;
import com.css.zfzx.data.primary.qhRiskLevelMapPointPG.repository.entity.QhRiskLevelMapPointPGEntity;
import com.css.zfzx.data.primary.qhRiskLevelMapPolygonPG.repository.QhRiskLevelMapPolygonPGNativeRepository;
import com.css.zfzx.data.primary.qhRiskLevelMapPolygonPG.repository.entity.QhRiskLevelMapPolygonPGEntity;
import com.css.zfzx.data.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * @author lxg
 * @date 2020-11-10
 */
@Repository
@PropertySource("classpath:platform-config.yml")
public class QhRiskLevelMapPolygonPGNativeRepositoryImpl implements QhRiskLevelMapPolygonPGNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(QhRiskLevelMapPolygonPGNativeRepositoryImpl.class);

    @Override
    public List<String> findAreaCreate(LocalDateTime createTime) {
        StringBuilder sql = new StringBuilder("select area from qh_risk_level_map_polygon ");
        StringBuilder whereSql = new StringBuilder(" where is_valid = 1 ");
        if (createTime!=null) {
            whereSql.append(" and create_time >:createTime");
        }else {
            whereSql.append(" and create_time is not null");
        }
        whereSql.append(" and update_time is null");
        whereSql.append(" group by province,city,area");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString());
        if (createTime!=null) {
            query.setParameter("createTime", createTime);
        }
        List<String> list = query.getResultList();
        return list;
    }

    @Override
    public List<QhRiskLevelMapPolygonPGEntity> findEntityCreate(LocalDateTime createTime, String area) {
        StringBuilder sql = new StringBuilder("select * from qh_risk_level_map_polygon ");
        StringBuilder whereSql = new StringBuilder(" where is_valid = 1 ");
        if (createTime!=null) {
            whereSql.append(" and create_time >:createTime");
        }else {
            whereSql.append(" and create_time is not null");
        }
        if (StringUtils.isNotEmpty(area)&&!"null".equals(area)){
            whereSql.append(" and area =:area");
        }
        whereSql.append(" and update_time is null");
        whereSql.append(" order by province,city,area");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), QhRiskLevelMapPolygonPGEntity.class);
        if (createTime!=null) {
            query.setParameter("createTime", createTime);
        }
        if (area!=null) {
            query.setParameter("area", area);
        }
        List<QhRiskLevelMapPolygonPGEntity> list = query.getResultList();
        return list;
    }

    @Override
    public List<String> findAreaUpdate(LocalDateTime updateTime) {
        StringBuilder sql = new StringBuilder("select area from qh_risk_level_map_polygon ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (updateTime!=null) {
            whereSql.append(" and update_time >:updateTime");
        }else {
            whereSql.append(" and update_time is not null");
            whereSql.append(" and is_valid=1");
        }
        whereSql.append(" group by province,city,area");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString());
        if (updateTime!=null) {
            query.setParameter("updateTime", updateTime);
        }
        List<String> list = query.getResultList();
        return list;
    }

    @Override
    public List<QhRiskLevelMapPolygonPGEntity> findEntityUpdate(LocalDateTime updateTime, String area) {
        StringBuilder sql = new StringBuilder("select * from qh_risk_level_map_polygon ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (updateTime!=null) {
            whereSql.append(" and update_time >:updateTime");
        }else {
            whereSql.append(" and update_time is not null");
            whereSql.append(" and is_valid=1");
        }
        if (StringUtils.isNotEmpty(area)&&!"null".equals(area)){
            whereSql.append(" and area =:area");
        }
        //whereSql.append(" and (uuid='be5d44ba0a5e4303b7da8202d6a9de4b' or uuid='fdd377839409435594b7a1eb6dd443b7')");
        whereSql.append(" order by province,city,area");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), QhRiskLevelMapPolygonPGEntity.class);
        if (updateTime!=null) {
            query.setParameter("updateTime", updateTime);
        }
        if (area!=null) {
            query.setParameter("area", area);
        }
        List<QhRiskLevelMapPolygonPGEntity> list = query.getResultList();
        return list;
    }
}
